package org.jeecg.modules.settlement;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.tuple.Pair;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.modules.doctor.vo.DoctorAccountInfoReqVo;
import org.jeecg.common.modules.order.entity.OrderInfo;
import org.jeecg.common.modules.settlement.vo.SettlementReqVo;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.modules.settlement.entity.Settlement;
import org.jeecg.common.modules.settlement.service.ISettlementService;
import org.jeecg.modules.settlement.service.AdminSettlementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.vo.ResultVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: settlement
 * @Author: jeecg-boot
 * @Date:   2020-09-03
 * @Version: V1.0
 */
@Api(tags="settlement")
@RestController
@RequestMapping("/settlement/settlement")
@Slf4j

public class SettlementController extends JeecgController<Settlement, ISettlementService> {
	@Autowired
	private ISettlementService settlementService;

	@Resource
	private AdminSettlementService adminSettlementService;
	
	/**
	 * 分页列表查询
	 *
	 * @param settlement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "settlement-分页列表查询")
	@ApiOperation(value="settlement-分页列表查询", notes="settlement-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Settlement settlement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//分页数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//（1）先查询所有的用户信息
		String orderId = req.getParameter("orderId");
		String doctorName = req.getParameter("doctorName");
		String userName   = req.getParameter("userName");

		List<SettlementReqVo> settlementReqVos = settlementService.selectSettlementInfo(orderId,userName,doctorName,dataStart,pageSize);

		//（2）获取总条数
		Integer count = settlementService.getSettlementCount(orderId,doctorName,userName);

		//（3）组织分页的返回数据
		ResultVo vo = this.initResultVo(settlementReqVos,count,pageNo,pageSize);
		return Result.ok(vo);
	}

	 /**
	  * 组合返回的信息
	  * @param settlementReqVos
	  * @param count
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 private ResultVo initResultVo(List settlementReqVos, Integer count, Integer pageNo, Integer pageSize){
		 ResultVo vo = new ResultVo();
		 double dCount    = Double.valueOf(count);       //查询的数据总条数
		 double dPageSize = Double.valueOf(pageSize);    //每页显示的条数
		 int pages = (int) Math.ceil(dCount / dPageSize);//页码数量

		 vo.setCurrent(pageNo);
		 vo.setOrders(new ArrayList<>());
		 vo.setRecords(settlementReqVos);
		 vo.setPages(pages);
		 vo.setSize(pageSize);
		 vo.setTotal(count);
		 return vo;
	 }

	 /**
	  * 通过订单ID查询并结算
	  *
	  * @param orderInfo
	  * @return
	  */
	 @AutoLog(value = "settlement-通过订单id查询结算信息")
	 @ApiOperation(value="settlement-通过订单id查询结算信息", notes="settlement-通过订单id查询结算信息")
	 @PostMapping(value = "/setMoney")
	 public Result<?> setMoney(@RequestBody OrderInfo orderInfo) {
	 	 String orderId = orderInfo.getId();
	 	 Settlement set = settlementService.getSettlementInfo(orderId);
		 Pair<Boolean,String> res = adminSettlementService.settlement(set.getId(),set.getCreateBy());
		 return Result.ok(res);
	 }
	
	/**
	 *   添加
	 *
	 * @param settlement
	 * @return
	 */
	@AutoLog(value = "settlement-添加")
	@ApiOperation(value="settlement-添加", notes="settlement-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Settlement settlement) {
		settlementService.save(settlement);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param settlement
	 * @return
	 */
	@AutoLog(value = "settlement-编辑")
	@ApiOperation(value="settlement-编辑", notes="settlement-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Settlement settlement) {
		settlementService.updateById(settlement);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "settlement-通过id删除")
	@ApiOperation(value="settlement-通过id删除", notes="settlement-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		settlementService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "settlement-批量删除")
	@ApiOperation(value="settlement-批量删除", notes="settlement-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.settlementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "settlement-通过id查询")
	@ApiOperation(value="settlement-通过id查询", notes="settlement-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Settlement settlement = settlementService.getById(id);
		if(settlement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(settlement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param settlement
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Settlement settlement) {
        return super.exportXls(request, settlement, Settlement.class, "settlement");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Settlement.class);
    }

}
